<template>
  <div class="common-layout">
    <el-container>
      <el-aside width="null">
        <div class="logo" @click="$router.push('/welcome')">
          <!-- <img src="../assets/logo.png" /> -->
          <span class="title">月夕花晨</span>
        </div>
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          :collapse="isCollapse"
          router
        >
          <TreeMenu :userMenu="menuList" />
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <Header @changeCollapse="changeCollapse"></Header>
        </el-header>
        <el-main>
          <Tags />
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import requestApi from "../api/requestApi";
import { useTagsStore } from "../store/tags";
import { useRouter, useRoute, onBeforeRouteUpdate } from "vue-router";
import { classifyMenu } from "../untils/common";
import { AxiosResponse } from "axios";
import { User } from "@/types/type";
let isCollapse = ref<boolean>(false);
function changeCollapse(val: boolean) {
  isCollapse.value = val;
}
// 菜单列表
let menuList = ref<User[]>([]);
// 请求菜单数据
onMounted(async () => {
  const res = (await requestApi.getMenuList()) as AxiosResponse;
  let newarr: User[] = [];
  // 筛选菜单
  classifyMenu(newarr, res.data, "left");
  console.log(newarr);
  menuList.value = newarr;
});
const route = useRoute();
const TagsStore = useTagsStore();

onBeforeRouteUpdate((to, from) => {
  let title = to.meta.title as string;
  let path = to.path;
  TagsStore.$patch((state) => {
    let index = state.dynamicTags?.findIndex(
      (item) => item.path == to.path
    ) as number;
    if (index == -1) {
      state.dynamicTags.push({ title, path });
    }
  });
});


// 及时更新菜单  ？？？

</script>

<style scoped lang="postcss">
.el-container {
  height: 100vh;
  .el-main {
    padding-top: 0;
  }
}
.el-aside {
  background-color: skyblue;
  color: #fff;
  text-align: center;
  overflow-y: auto;
  .logo {
    height: 60px;
    @apply flex;
    align-items: center;
    justify-content: center;
  }
}
.el-header {
  background: grey;
  @apply flex;
  align-items: center;
  justify-content: space-between;
}
.el-menu {
  background: none;
  border: none;
}
.el-main {
  @apply flex flex-col;
  background: #e9eef4;
}
</style>
